//
//  ShopcartOrderTableViewCell.m
//  TerritoryShopkeeper
//
//  Created by 李小龙 on 2017/12/12.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "ShopcartOrderTableViewCell.h"
@interface ShopcartOrderTableViewCell ()<UITextViewDelegate>

@end

@implementation ShopcartOrderTableViewCell

#pragma mark - IBAction
- (IBAction)fifthTypeButtonAction:(id)sender {
    if (self.block) {
        self.block(ShopcartOrderClickTypeReceipt);
    }
}
- (IBAction)sixthEnterpriseButtonAction:(id)sender {
    if (!self.sixthEnterpriseButton.selected) {
        if (self.block) {
            self.block(ShopcartOrderClickTypeEnterprise);
        }
    }
    [self.sixthPersonButton setSelected:NO];
    [self.sixthEnterpriseButton setSelected:YES];
}
- (IBAction)sixthPersonButtonAction:(id)sender {
    if (!self.sixthPersonButton.selected) {
        if (self.block) {
            self.block(ShopcartOrderClickTypePerson);
        }
    }
    [self.sixthPersonButton setSelected:YES];
    [self.sixthEnterpriseButton setSelected:NO];
}
- (IBAction)eighthFrontButtonAction:(id)sender {
    if (self.block) {
        self.block(ShopcartOrderClickTypeFront);
    }
}
- (IBAction)eighthBackButtonAction:(id)sender {
    if (self.block) {
        self.block(ShopcartOrderClickTypeBack);
    }
}
#pragma mark -
+ (instancetype)shopcartOrderCellWith:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath count:(NSInteger)count {
    NSString *identifier;
    NSInteger index = 0;
    switch (indexPath.section) {
        case 0:
        {
            if (indexPath.row < count) {
                identifier = @"shopcartOrderIdentifierFirst";
                index = 0;
            } else if (indexPath.row == count) {
                identifier = @"shopcartOrderIdentifierSecond";
                index = 1;
            } else {
                identifier = @"shopcartOrderIdentifierThird";
                index = 2;
            }
        } break;
        case 1:
        {
            identifier = @"shopcartOrderIdentifierFourth";
            index = 3;
        } break;
        case 2:
        {
            switch (indexPath.row) {
                case 0:
                {
                    identifier = @"shopcartOrderIdentifierFifth";
                    index = 4;
                } break;
                case 1:
                {
                    identifier = @"shopcartOrderIdentifierSixth";
                    index = 5;
                } break;
                case 2: case 3:
                {
                    identifier = @"shopcartOrderIdentifierSeventh";
                    index = 6;
                } break;
                default:
                    break;
            }
        } break;
        case 3:
        {
            switch (indexPath.row) {
                case 0: case 1:
                {
                    identifier = @"shopcartOrderIdentifierSeventh";
                    index = 6;
                } break;
                case 2:
                {
                    identifier = @"shopcartOrderIdentifierEighth";
                    index = 7;
                } break;
                default:
                    break;
            }
        } break;
        default:
            break;
    }
    
    ShopcartOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ShopcartOrderTableViewCell" owner:self options:nil] objectAtIndex:index];
    }
    return cell;
}

- (void)updateGoodCellWith:(ShopcartModel *)model indexPath:(NSIndexPath *)indexPath count:(NSInteger)count {
    if (indexPath.section == 0 && indexPath.row < count) {
        [self.firstGoodImageView sd_setImageWithURL:GetUrl(model.gimg) placeholderImage:PlaceholderImageSmall changeContentModel:UIViewContentModeScaleAspectFill];
        [self.firstGoodNameLabel setText:model.gname];
        [self.firstGoodCountLabel setText:[NSString stringWithFormat:@"x%@",model.number]];
        [self.firstGoodPriceLabel setText:[NSString stringWithFormat:@"¥%@",model.gprice]];
        // 属性
        GoodsAttributePriceModel *temp;
        for (GoodsAttributePriceModel *attModel in model.goodsattribute) {
            if ([model.gaid isEqualToString:attModel.gaid]) {
                temp = attModel;
                break;
            }
        }
        NSString *specs = @"";
        if (temp) {
            for (NSInteger i = 0; i < temp.twoattribute.count; i++) {
                TwoAtrributeModel *twoAtt = temp.twoattribute[i];
                if (i == 0) {
                    specs = twoAtt.taname;
                } else {
                    specs = [NSString stringWithFormat:@"%@;%@",specs,twoAtt.taname];
                }
            }
        }
        [self.firstGoodSpecsLabel setText:specs];
    }
}

+ (instancetype)shopcartOrderCellWith:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath goodsCount:(NSInteger)count classifyCount:(NSInteger)classifyCount {
    NSString *identifier;
    NSInteger index = 0;
    if (indexPath.section == classifyCount) {
        // 邮费
        identifier = @"shopcartOrderIdentifierThird";
        index = 2;
    } else if (indexPath.section == classifyCount+1) {
        // 发票
        switch (indexPath.row) {
            case 0:
            {
                identifier = @"shopcartOrderIdentifierFifth";
                index = 4;
            } break;
            case 1:
            {
                identifier = @"shopcartOrderIdentifierSixth";
                index = 5;
            } break;
            case 2: case 3:
            {
                identifier = @"shopcartOrderIdentifierSeventh";
                index = 6;
            } break;
            default:
                break;
        }
    } else if (indexPath.section == classifyCount+2) {
        // 身份证
        switch (indexPath.row) {
            case 0: case 1:
            {
                identifier = @"shopcartOrderIdentifierSeventh";
                index = 6;
            } break;
            case 2:
            {
                identifier = @"shopcartOrderIdentifierEighth";
                index = 7;
            } break;
            default:
                break;
        }
    } else {
        // 商品+备注+优惠券+满减
        if (indexPath.row == count*2) {
            identifier = @"shopcartOrderIdentifierSecond";
            index = 1;
        } else if (indexPath.row == count*2+1) {
            identifier = @"shopcartOrderIdentifierNinth";
            index = 8;
        } else if (indexPath.row%2 == 0) {
            identifier = @"shopcartOrderIdentifierFirst";
            index = 0;
        } else if (indexPath.row%2 == 1) {
            identifier = @"shopcartOrderIdentifierFourth";
            index = 3;
        }
    }
    
    ShopcartOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ShopcartOrderTableViewCell" owner:self options:nil] objectAtIndex:index];
    }
    return cell;
}

- (void)updateGoodCellWith:(ShopcartModel *)model classifyModel:(ShopcartClassifyModel *)classifyModel indexPath:(NSIndexPath *)indexPath goodsCount:(NSInteger)count classifyCount:(NSInteger)classifyCount {
    if (indexPath.section < classifyCount) {
        // 商品+备注+优惠券+满减
        if (indexPath.row == count*2) {
            // 优惠券
            if (classifyModel.couponModel) {
                [self.secondCountLabel setHidden:NO];
                [self.secondPriceLabel setHidden:NO];
                [self.secondPriceLabel setText:[NSString stringWithFormat:@"-¥%@",classifyModel.couponModel.creduce]];
            } else {
                [self.secondCountLabel setHidden:YES];
                [self.secondPriceLabel setHidden:YES];
                [self.secondPriceLabel setText:@""];
            }
        } else if (indexPath.row == count*2+1) {
            // 满减（同优惠券）
            NSString *cutStr;
            if (classifyModel.couponModel) {
                cutStr = @"此价格不与套餐优惠同时享受";
            } else if (classifyModel.fullcut.count > 0) {
                double classTotal = 0;
                for (ShopcartModel *model in classifyModel.shopcart) {
                    classTotal += model.gprice.doubleValue*model.number.integerValue;
                }
                for (GoodsFullCut *cutModel in classifyModel.fullcut) {
                    if (classTotal > cutModel.fcfull.doubleValue) {
                        cutStr = [NSString stringWithFormat:@"满%@减%@",cutModel.fcfull,cutModel.fccut];
                        break;
                    }
                }
            } else {
                cutStr = @"暂无促销活动";
            }
            [self.ninthPriceLabel setText:cutStr];
        } else if (indexPath.row%2 == 0) {
            [self.firstGoodImageView sd_setImageWithURL:GetUrl(model.gimg) placeholderImage:PlaceholderImageSmall changeContentModel:UIViewContentModeScaleAspectFill];
            [self.firstGoodNameLabel setText:model.gname];
            [self.firstGoodCountLabel setText:[NSString stringWithFormat:@"x%@",model.number]];
            NSString *price;
            if (model.isigoods) {
                price = [NSString stringWithFormat:@"%@积分",model.gintegral];
            } else {
                price = [NSString stringWithFormat:@"¥%@",model.gprice];
            }
            [self.firstGoodPriceLabel setText:price];
            // 属性
            GoodsAttributePriceModel *temp;
            for (GoodsAttributePriceModel *attModel in model.goodsattribute) {
                if ([model.gaid isEqualToString:attModel.gaid]) {
                    temp = attModel;
                    break;
                }
            }
            NSString *specs = @"";
            if (temp) {
                for (NSInteger i = 0; i < temp.twoattribute.count; i++) {
                    TwoAtrributeModel *twoAtt = temp.twoattribute[i];
                    if (i == 0) {
                        specs = twoAtt.taname;
                    } else {
                        specs = [NSString stringWithFormat:@"%@;%@",specs,twoAtt.taname];
                    }
                }
            }
            [self.firstGoodSpecsLabel setText:specs];
        } else if (indexPath.row%2 == 1) {
            // 备注
            self.fourthTextView.delegate = self;
            [self.fourthTextView setText:model.note];
        }
    }
}

#pragma mark - UITextViewDelegate
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
    if (self.noteBlock) {
        self.noteBlock(textView.text);
    }
    return YES;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
